package com.hjqjl.module_main.ui;

import android.view.KeyEvent;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hjqjl.common_lib.base.ActivityCollector;
import com.hjqjl.module_main.R;
import com.hjqjl.module_main.databinding.MainActivityMainBinding;
import com.hjqjl.whapplib.base.BaseActivity;
import com.hjqjl.whapplib.router.RouterPath;

import java.util.ArrayList;
import java.util.List;

// 在支持路由的页面上添加注解(必选)
// 这里的路径需要注意的是至少需要有两级，/xx/xx
@Route(path = RouterPath.MAIN_MAIN)
public class MainActivity extends BaseActivity<MainActivityMainBinding> {
    private List<Fragment> mFragments;
    public Fragment mHomeFragment;
    public Fragment mChoiceFragment;
    public Fragment mMineFragment;

    @Override
    public void initData() {
        mFragments = new ArrayList<>();
        setTabSelection(0);//默认选择首页
    }

    @Override
    public void initListener() {
        pVBinding.mainLlMainTabHome.setOnClickListener(v -> setTabSelection(0));
        pVBinding.mainLlMainTabChoice.setOnClickListener(v -> setTabSelection(1));
        pVBinding.mainLlMainTabMine.setOnClickListener(v -> setTabSelection(2));
    }

    /**
     * 根据传入的index参数来设置选中的tab页。
     *
     * @param index 每个tab页对应的下标。
     */
    private void setTabSelection(int index) {
        LogUtils.v("index:" + index);
        // 每次选中之前先清楚掉上次的选中状态
        clearSelection();
        // 开启一个Fragment事务
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (index) {
            case 0://首页
                LogUtils.v("switch:" + index);
                pVBinding.mainLlMainTabHome.setSelected(true);
                if (mHomeFragment == null) {
                    // 如果mHomeFragment为空，则创建一个并添加到界面上
                    mHomeFragment = (Fragment) ARouter.getInstance().build(RouterPath.HOME_HOME_FRAGMENT).navigation();
                    transaction.add(R.id.main_fl_main_content, mHomeFragment);
                    mFragments.add(mHomeFragment);
                } else {
                    // 如果mHomeFragment不为空，则直接将它显示出来
                    transaction.show(mHomeFragment);
                }
                break;
            case 1://精选
                LogUtils.v("switch:" + index);
                pVBinding.mainLlMainTabChoice.setSelected(true);
                if (mChoiceFragment == null) {
                    // 如果mChoiceFragment为空，则创建一个并添加到界面上
                    mChoiceFragment = (Fragment) ARouter.getInstance().build(RouterPath.CHOICE_CHOICE_FRAGMENT).navigation();
                    transaction.add(R.id.main_fl_main_content, mChoiceFragment);
                    mFragments.add(mChoiceFragment);
                } else {
                    // 如果mChoiceFragment不为空，则直接将它显示出来
                    transaction.show(mChoiceFragment);
                }
                break;
            case 2://我的
                LogUtils.v("switch:" + index);
                pVBinding.mainLlMainTabMine.setSelected(true);
                if (mMineFragment == null) {
                    // 如果mMineFragment为空，则创建一个并添加到界面上
                    mMineFragment = (Fragment) ARouter.getInstance().build(RouterPath.MINE_MINE_FRAGMENT).navigation();
                    transaction.add(R.id.main_fl_main_content, mMineFragment);
                    mFragments.add(mMineFragment);
                } else {
                    // 如果mMineFragment不为空，则直接将它显示出来
                    transaction.show(mMineFragment);
                }
                break;
            default:

        }
        transaction.commit();
    }

    /**
     * 清除掉所有的选中状态。
     */
    private void clearSelection() {
        pVBinding.mainLlMainTabHome.setSelected(false);
        pVBinding.mainLlMainTabChoice.setSelected(false);
        pVBinding.mainLlMainTabMine.setSelected(false);
    }

    /**
     * 隐藏掉所有的Fragment
     */
    private void hideFragments(FragmentTransaction transaction) {
        for (Fragment fragment : mFragments) {
            if (fragment != null && !fragment.isHidden()) {
                transaction.hide(fragment);
            }
        }
    }


    private long mExitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            //两秒之内按返回键就会退出
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                ToastUtils.showShort("再按一次退出程序");
                mExitTime = System.currentTimeMillis();
            } else {
//                ActivityCollector.getInstance().appExit();
                AppUtils.exitApp();// 退出时特别生硬
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
